package com.qian.ddj.pay.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.qian.api.ddj.util.SignUtils;
import com.qian.ddj.pay.util.HttpRequestUtil;
import com.qian.ddj.pay.util.MD5Utils;
import com.yaowk.api.utils.HttpUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;

/**
 * created by quziwei on 2018/2/27 0027  13:41
 */
@Controller
@RequestMapping("/test")
public class TestController {
    static String signKey = "d5983e1306bf41f5bf6ea98715ff40c2";
    private static String partner ="1000158419";

    @RequestMapping
    public void test(HttpServletResponse response){
        System.out.println("下单。。。。。。。。。。。。。。");
        String pid = partner;
        String gateWay = "http://120.77.22.14:8380/pay/gateway/unify.do";//请求
        SortedMap<String, String> requestMap = new TreeMap<String, String>();
        requestMap.put("TRDE_CODE", "20001");//交易码
        requestMap.put("PRT_CODE", pid);//机构号
        requestMap.put("VER_NO", "1.0");//版本号  1.0
        requestMap.put("MERC_ID", "834584047330002");//进件成功后返回的商户号
        requestMap.put("BIZ_CODE", "3007");//微信1003 支付宝2003QQ扫码5003 3003网银
        requestMap.put("ORDER_NO", "cs"+System.currentTimeMillis());//订单号  确保接入系统唯一
        requestMap.put("TXN_AMT", "2.00");//订单金额
        requestMap.put("PRO_DESC", "123123");//商品描述

        requestMap.put("CARD_NO", "");//网银支付用
        requestMap.put("BNK_CD", "307");//网银支付用  银行编码
        requestMap.put("ACC_TYP", "");//网银支付用  借贷标记

        requestMap.put("NOTIFY_URL", "http://notify.url");//异步通知地址
        String nonce_str = UUID.randomUUID().toString().replaceAll("-", "");// 生成随机字符串
        requestMap.put("NON_STR", nonce_str);//随机字符串
        requestMap.put("TM_SMP", System.currentTimeMillis()+"");//时间戳
        requestMap.put("SIGN_TYP", "MD5");//签名方式
        String sign = MD5Utils.getSign(requestMap, signKey);
        System.out.println("数据签名成功 sign=" + sign);
        requestMap.put("SIGN_DAT", sign);//签名数据

        String json = JSON.toJSONString(requestMap);
        String ret = "";
        try {
            ret = HttpRequestUtil.httpPostSendReceive(gateWay, json);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        JSONObject jsonobj = JSON.parseObject(ret);
        String status = (String) jsonobj.get("RETURNCODE");
        String message = (String) jsonobj.get("RETURNCON");

        if (!status.equalsIgnoreCase("0000")) {
            System.out.println("失败描述："+message);
            return;
        }

        System.out.println("数据签名验证");
        if (!MD5Utils.checkSign(jsonobj, signKey)) {
            System.out.println("数据签名验证错误");
            return;
        }
        System.out.println("数据签名验证成功");

        String qrCode = (String) jsonobj.get("QR_CODE");
        String mch_id = (String) jsonobj.get("MERC_ID");
        String prt_code = (String) jsonobj.get("PRT_CODE");
        String out_trade_no = (String) jsonobj.get("OUT_TRADE_NO");
        System.out.println("返回url："+qrCode);
        System.out.println("返回外部订单号："+out_trade_no);
        System.out.println("返回商户号："+mch_id);
        System.out.println("返回机构号："+prt_code);
        try {
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().print(jsonobj.getString("RET_HTML"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
